﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Web;
using Ext.Net;
using Viewer.History.BusinessObject.BusinessEntities;


namespace Viewer.History.Pages
{
    public partial class History : BusinessPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                QuanLiLogDataContext context = new QuanLiLogDataContext();
                List<Viewer.History.BusinessObject.BusinessEntities.HistoryGeneral> historyGenerals = context.sp_Log_GetGeneralLogs(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31));
                GrdMainGrid.SetDataSource(historyGenerals);
            }
        }


        public override void InitializeControls()
        {
            base.InitializeControls();
            GrdMainGrid.GridID = (int)Viewer.Base.Config.ViewerConstant.GridID.GRIDID_HISTORY_GENERAL;
            
            GrdMainGrid.EnableDownloadCSV = true;
            GrdMainGrid.EnableDownloadExcel = true;
            GrdMainGrid.EnableDownloadXML = true;
            //GrdMainGrid.AddGridColumn("LogID", "LogID", 50);
            //GrdMainGrid.AddGridColumn("UserID", "Người Dùng", 100);
            GrdMainGrid.AddGridColumn("UserName", "Người Dùng", 100);
            GrdMainGrid.AddGridColumn("FunctionID", "Chức Năng", 100);
            GrdMainGrid.AddGridColumn("ActionTypeName", "Loại Thao Tác", 150);
            GrdMainGrid.AddGridDateColumn("UpdateDateTime", "Thời Gian", "dd/MM/yyyy", 200);
            GrdMainGrid.AddGridColumn("TableName", "Bảng", 100);
            GrdMainGrid.AddGridColumn("TableLogID", "ID", 50);
            GrdMainGrid.PageSize = 20;
            //GrdMainGrid.SetAutoExpandColumn("UpdateDateTime");

            SetRecordField();            

            //must call
            GrdMainGrid.BuildGrid();
        }

        public override void LoadData()
        {

        }

        private void SetRecordField()
        {
            //GrdMainGrid.AddRecordField(new Ext.Net.RecordField("LogID", Ext.Net.RecordFieldType.Int));
            GrdMainGrid.AddRecordField(new Ext.Net.RecordField("UserID", Ext.Net.RecordFieldType.Int));
            GrdMainGrid.AddRecordField(new Ext.Net.RecordField("UserName", Ext.Net.RecordFieldType.String));
            GrdMainGrid.AddRecordField(new Ext.Net.RecordField("FunctionID", Ext.Net.RecordFieldType.String));
            GrdMainGrid.AddRecordField(new Ext.Net.RecordField("ActionTypeID", Ext.Net.RecordFieldType.Int));
            GrdMainGrid.AddRecordField(new Ext.Net.RecordField("ActionTypeName", Ext.Net.RecordFieldType.String));
            GrdMainGrid.AddRecordField(new Ext.Net.RecordField("UpdateDateTime", Ext.Net.RecordFieldType.Date));
            GrdMainGrid.AddRecordField(new Ext.Net.RecordField("TableName", Ext.Net.RecordFieldType.String));
            GrdMainGrid.AddRecordField(new Ext.Net.RecordField("TableLogID", Ext.Net.RecordFieldType.Int));            
        }

        public override void AddEvent()
        {
            UCSimpleDetailBase1.ShowDialog(0, Viewer.Base.Config.ViewerConstant.DetailsFormType.Add);
        }

        public override void DeleteEvent(int id)
        {
            //UCSimpleDetailBase1.ShowDialog(0, Viewer.Base.Config.ViewerConstant.DetailsFormType.Add);
            UCSimpleDetailBase1.DoDelete(id);
        }

        public override void EditEvent(int id)
        {
            UCSimpleDetailBase1.ShowDialog(id, Viewer.Base.Config.ViewerConstant.DetailsFormType.Edit);
        }

        public override void DetailEvent(int id)
        {
            UCSimpleDetailBase1.ShowDialog(id, Viewer.Base.Config.ViewerConstant.DetailsFormType.View);
        }

        public override void CopyEvent(int id)
        {
            UCSimpleDetailBase1.ShowDialog(id, Viewer.Base.Config.ViewerConstant.DetailsFormType.Copy);
        }

        public override void Search()
        {
            string strWhere = string.Empty;
            if (this.cboXepHang.SelectedIndex != -1)
            {
                if (strWhere.Length > 0)
                {
                    strWhere += " AND ";
                }
                strWhere += string.Format("XepHangDonViID={0}", this.cboXepHang.SelectedItem.Value);
            }

            if (this.cboKhoi.SelectedIndex != -1)
            {
                if (strWhere.Length > 0)
                {
                    strWhere += " AND ";
                }
                strWhere += string.Format("KhoiQuanLyID = {0}", this.cboKhoi.SelectedItem.Value);
            }

            if (this.cboLoaiToChuc.SelectedIndex != -1)
            {
                if (strWhere.Length > 0)
                {
                    strWhere += " AND ";
                }
                strWhere += string.Format("LoaiToChucID = {0}", this.cboLoaiToChuc.SelectedItem.Value);
            }

            if (!string.IsNullOrEmpty(this.txtTenToChuc.Text.Trim()))
            {
                if (strWhere.Length > 0)
                {
                    strWhere += " AND ";
                }
                strWhere += string.Format("TenToChucShort.Contains(\"{0}\")", this.txtTenToChuc.Text.Trim());
            }           

            QuanLiLogDataContext context = new QuanLiLogDataContext();
            List<Viewer.History.BusinessObject.BusinessEntities.HistoryGeneral> historyGenerals = context.sp_Log_GetGeneralLogs(new DateTime(2010, 1, 1), new DateTime(2011, 12, 31));
            GrdMainGrid.SetDataSource(historyGenerals);
        }

        protected void btnSearch_Click(object sender, DirectEventArgs e)
        {
            this.Search();
        }
        
    }
}